﻿using ShellProgressBar;
using System;
using System.IO;
using System.Text;

namespace TextProcessingLibrary
{
    public static class TextHelper
    {
        private static ProgressBar _progressBar;
        private static int _stepRead = 128;
        private static int _length;


        public static string LoadWithProgress(string path)
        {
            string result;

            using (FileStream inputFileStream = File.OpenRead(path))
            {
                using (var pStream = new ProgressStream(inputFileStream))
                {
                    _length = (int)pStream.Length;
                    _progressBar = new ProgressBar(_length / (1024 * _stepRead), "Reading input from " + path, ConsoleColor.Cyan);
                    pStream.BytesRead += tick;

                    using (StreamReader reader = new StreamReader(pStream))
                        result = reader.ReadToEnd();
                }
            }

            Console.WriteLine();
            Console.WriteLine();
            return result;
        }

        public static T DeserializeWithProgress<T>(string path, ISerializer serializer)
        {
            T result;

            using (var inputFileStream = File.OpenRead(path))
            {
                using (var pStream = new ProgressStream(inputFileStream))
                {
                    _length = (int)pStream.Length;
                    _progressBar = new ProgressBar(_length / (1024 * _stepRead), "Reading input from " + path, ConsoleColor.Cyan);
                    pStream.BytesRead += tick;

                    result = serializer.Deserialize<T>(pStream);
                }
            }

            Console.WriteLine();
            Console.WriteLine();
            return result;
        }

        public static void SaveWithProgress(string path, string text, Encoding outputEncoding)
        {
            using (FileStream outputFileStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
            {
                _length = outputEncoding.GetBytes(text).Length;

                using (var pStream = new ProgressStream(outputFileStream))
                {
                    _progressBar = new ProgressBar(_length / (1024 * _stepRead), "Writing output to " + path, ConsoleColor.Cyan);
                    pStream.BytesWritten += tick;

                    using (StreamWriter writer = new StreamWriter(pStream, outputEncoding))
                        writer.Write(text);
                }
            }

            Console.WriteLine();
            Console.WriteLine();
        }


        private static void tick(object sender, ProgressStreamReportEventArgs args)
        {
            var percentage = ((double)args.StreamPosition / _length);
            var currentTicks = _progressBar.MaxTicks * percentage;
            if (currentTicks - _progressBar.CurrentTick >= 1)
                _progressBar.Tick();
        }
    }
}

